package com.yhxd.modular.institutions.controller;

import com.querydsl.core.types.Order;
import com.querydsl.core.types.OrderSpecifier;
import com.yhxd.modular.institutions.entity.Dept;
import com.yhxd.modular.institutions.entity.QDept;
import com.yhxd.modular.institutions.service.DeptService;
import com.yhxd.modular.system.entity.Resource;
import com.yhxd.modular.system.vo.ResultVo;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 机构管理>>单位机构
 * @author lzl
 * @date 2020/12/22 16:06
 */
@Controller
@RequestMapping(value = "/ins/dept")
@Api
public class DeptController {


    @Autowired
    private DeptService deptService;

    /**
     * 跳转到列表页
     * @return
     */
    @GetMapping(value = "/list")
    @RequiresPermissions("dept_list")
    public String list() {
        return "mng/dept/dept_list";
    }


    /**
     * 获取部门列表信息
     * @param page
     * @param limit
     * @param dto
     * @return
     */
    @PostMapping(value = "/dept_list")
    @RequiresPermissions("dept_list")
    @ResponseBody
    public ResultVo findList(Integer page, Integer limit,Dept dto) {
        try {
            QPageRequest pageRequest = QPageRequest.of(page - 1, limit, new OrderSpecifier(Order.DESC, QDept.dept.id));
            Page<Dept> resourcePage = this.deptService.findList(pageRequest, dto);
            return ResultVo.success(resourcePage);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.error(e.getMessage());
        }
    }

    /**
     * 查询所有部门数据
     * @return
     */
    @GetMapping(value = "/findAll")
    @ResponseBody
    public ResultVo findAll(Long id) {
        try {
            List<Dept> resourceList = this.deptService.findAll();
            Dept dept = new Dept();
            dept.setId(0L);
            dept.setDepName("深圳市不动产登记中心");
            resourceList.add(dept);
            return ResultVo.success(resourceList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.error(e.getMessage());
        }
    }

    /**
     * 跳转到新增页
     * @return
     */
    @GetMapping(value = "/add")
    @RequiresPermissions("dept_add")
    public String add(Model model) {
        return "mng/dept/dept_add";
    }
    /**
     * 保存部门信息
     * @return
     */
    @PostMapping(value = "/save")
    @RequiresPermissions("dept_add")
    @ResponseBody
    public ResultVo save(Dept dept) {
        try {
            this.deptService.saveOrUpdate(dept);
            return ResultVo.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.error(e.getMessage());
        }
    }
    /**
     * 跳转到修改页
     * @return
     */
    @GetMapping(value = "/edit")
    @RequiresPermissions("dept_edit")
    public ModelAndView edit(ModelAndView model,Long id) {
        model.addObject("dept",this.deptService.findById(id));
        model.setViewName("mng/dept/dept_edit");
        return model;
    }
    /**
     * 修改部门信息
     * @return
     */
    @PostMapping(value = "/update")
    @RequiresPermissions("dept_edit")
    @ResponseBody
    public ResultVo update(Dept dept) {
        try {
            return this.deptService.saveOrUpdate(dept);
        } catch (Exception e) {
            e.printStackTrace();
           return ResultVo.error(e.getMessage());
        }
    }
    /**
     * 修改部门状态
     * @return
     */
    @PostMapping(value = "/updateStatus")
    @RequiresPermissions("dept_edit")
    @ResponseBody
    public ResultVo updateStatus(Dept dept) {
        try {
            return this.deptService.updateStatus(dept);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.error(e.getMessage());
        }
    }

    /**
     * 通过id删除部门
     * @return
     */
    @GetMapping(value = "/delete/{id}")
    @RequiresPermissions("dept_delete")
    @ResponseBody
    public ResultVo delete(@PathVariable("id") Long id){
        try {
            if(this.deptService.isRelateNews(id)) {
                return ResultVo.error("机构关联新闻信息，不能删除！");
            }else{
                this.deptService.deleteById(id);
            }
            return ResultVo.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.error(e.getMessage());
        }
    }

}
